package com.gali.leetcode.day17;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: gali
 * @Date: 2022-08-03 19:43
 * @Description:
 */
public class Solution {
    /**
     * 跳跃游戏---贪心
     * @param nums
     * @return
     */
    public boolean canJump(int[] nums) {
        int length = nums.length;
        int max=0;
        for (int i = 0; i < length; i++) {
            if (i<=max){
                max=Math.max(max,i+nums[i]);
                if (max>=length-1){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 合并区间
     * @param intervals
     * @return
     */
    public int[][] merge(int[][] intervals) {
        List<int[]> list = new ArrayList<>();
        //排序
        Arrays.sort(intervals,(x,y)-> x[0]-y[0]);
        int l=intervals[0][0];
        int r=intervals[0][1];
        for (int i = 0; i < intervals.length; i++) {
            if (intervals[i][0]>r){
                list.add(new int[]{l,r});
                l=intervals[i][0];
                r=intervals[i][1];
            }else {
                r=Math.max(r,intervals[i][1]);
            }
        }
        list.add(new int[]{l,r});
        return list.toArray(new int[0][]);
    }
}
